package com.imarkket.Library;

import android.content.Context;
import android.content.SharedPreferences;

import com.imarkket.c_login_activity;

/**
 * Created by Sistemas on 23/05/2014.
 */
public class c_preferences {

    private static final String APP_PREFERENCIA = "APP_CONFIG";
    private SharedPreferences appSharedPrefs;
    private SharedPreferences.Editor prefsEditor;
    private String usernamePref = "username";
    private String passwordPref = "password";
    private String checkRemenberPref = "checkRemenberPref";
    private String checkAutoLoginPref = "checkAutoLoginPref";

    private String usernamePrefTemp = "username_temp";
    private String passwordPrefTemp = "password_temp";

    public c_preferences(Context context) {
        this.appSharedPrefs = context.getSharedPreferences(APP_PREFERENCIA, c_login_activity.MODE_PRIVATE);
        this.prefsEditor = appSharedPrefs.edit();
    }

    public void setUsername(String username) {
        prefsEditor.putString(usernamePref, username).commit();
    }

    public void setPassword(String password) {
        prefsEditor.putString(passwordPref, password).commit();
    }

    public void setUsernameTemp(String username) {
        prefsEditor.putString(usernamePrefTemp, username).commit();
    }

    public void setPasswordTemp(String password) {
        prefsEditor.putString(passwordPrefTemp, password).commit();
    }

    public Boolean getCheckRemenberPref() {
        return appSharedPrefs.getBoolean(checkRemenberPref, false);
    }

    public void setCheckRemenberPref(Boolean checkRemenber) {
        prefsEditor.putBoolean(checkRemenberPref, checkRemenber).commit();
    }

    public String getUsername(Boolean isLoadingApp) {
        if ((this.getCheckRemenberPref()) || (isLoadingApp)) {
            return appSharedPrefs.getString(usernamePref, "default@app.com");
        } else {
            return appSharedPrefs.getString(usernamePrefTemp, "temporal@app.com");
        }
    }

    public String getPassword(Boolean isLoadingApp) {
        // isLoadinApp se utiliza para poner los valores por defecto si está entrando a la app o si está el check activdo
        if ((this.getCheckRemenberPref()) || (isLoadingApp)) {
            return appSharedPrefs.getString(passwordPref, "default");
        } else {
            return appSharedPrefs.getString(passwordPrefTemp, "temporal");
        }
    }

    public String getPasswordTemp() {
        return appSharedPrefs.getString(passwordPrefTemp, "temporal");
    }

    public String getUsernameTemp() {
        return appSharedPrefs.getString(usernamePrefTemp, "temporal@app.com");
    }

    public Boolean getAutoLogin() {
        return appSharedPrefs.getBoolean(checkAutoLoginPref, false);
    }

    public void setAutoLogin(Boolean checkAutoLogin) {
        prefsEditor.putBoolean(checkAutoLoginPref, checkAutoLogin).commit();
    }

}
